package com.me.modules.sla.controller;

import com.me.modules.common.annotation.SysLog;
import com.me.modules.common.controller.BaseController;
import com.me.modules.common.page.Page;
import com.me.modules.common.utils.Result;
import com.me.modules.common.utils.Utils;
import com.me.modules.sla.entity.SlaAlertDefEntity;
import com.me.modules.sla.entity.SlaAlertTempEntity;
import com.me.modules.sla.service.SlaAlertDefService;
import com.me.modules.sla.service.SlaAlertTempService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;


/**
 * 服务级别协议告警策略
 *
 * @author lill
 * @email lill5270@163.com
 * @date 2019-01-11 10:09:38
 */
@Controller
@RequestMapping("/sla/slaalerttemp")
public class SlaAlertTempController extends BaseController{
	@Autowired
	private SlaAlertTempService slaAlertTempService;
	@Autowired
	private SlaAlertDefService slaAlertDefService;

	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("slaalerttemp:list")
	@SysLog("查询SLA告警策略列表")
	public String  list(SlaAlertTempEntity slaalerttemp, Model model , HttpServletRequest request){
		//查询列表数据
		int pageNum = Utils.parseInt(request.getParameter("pageNum"), 1);
		Page<SlaAlertTempEntity> page = slaAlertTempService.findPage(slaalerttemp,pageNum);

		//PageUtils pageUtil = new PageUtils(timetblInfoList, total, query.getLimit(), query.getPage());
		model.addAttribute("slaalerttemp",slaalerttemp);
		model.addAttribute("page",page);
		return "sla/sla_alert_temp/list";
	}


	/**
	 * 信息
	 */
	@RequestMapping("/info")
	//@RequiresPermissions("slalevel:info")
	@SysLog("查询SLA告警策略详情")
	public String info( Model model,Long id, HttpServletRequest request){
		SlaAlertTempEntity slaalerttemp = slaAlertTempService.queryObject(id);
		model.addAttribute("slaalerttemp",slaalerttemp);
		return "sla/sla_alert_temp/edit";
	}



	@RequestMapping(value = "save",method = RequestMethod.POST)
//	@RequiresPermissions("timetblinfo:save")
	@ResponseBody
	public Result save(SlaAlertTempEntity slaalerttemp,Long[] slaAlertDefIds){
		if(slaalerttemp.getId()==null) {
			slaAlertTempService.save(slaalerttemp);
		}else {
			slaAlertTempService.update(slaalerttemp);
		}
		if (slaAlertDefIds!=null){
			for (Long id : slaAlertDefIds) {
				SlaAlertDefEntity slaAlertDef = slaAlertDefService.queryObject(id);
				slaAlertDef.setPolicyId(slaalerttemp.getId());
				slaAlertDefService.update(slaAlertDef);
			}
		}
		return Result.ok();
	}


	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("slaalerttemp:update")
	public Result update(@RequestBody SlaAlertTempEntity slaAlertTemp){
		slaAlertTempService.update(slaAlertTemp);

		return Result.ok();
	}

	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("slaalerttemp:delete")
	public Result delete(@RequestBody Long[] ids){
		slaAlertTempService.deleteBatch(ids);

		return Result.ok();
	}

}
